1
|
|
|
import dayjs from "dayjs"; |
2
|
|
|
import { Json } from "../hooks/webResourceHooks/types"; |
3
|
|
|
|
4
|
|
|
type HttpVerb = "GET" | "POST" | "PUT" | "DELETE"; |
5
|
|
|
|
6
|
|
|
const csrfElement = document.head.querySelector('meta[name="csrf-token"]'); |
7
|
|
|
const csrfToken: string = |
8
|
|
|
csrfElement && csrfElement.getAttribute("content") |
9
|
|
|
? (csrfElement.getAttribute("content") as string) |
10
|
|
|
: ""; |
11
|
|
|
|
12
|
|
|
function jsonDateReplacer(key, value): string | any { |
13
|
|
|
if (this[key] instanceof Date) { |
14
|
|
|
return dayjs(value).format("YYYY-MM-DDTHH:mm:ssZ"); |
15
|
|
|
} |
16
|
|
|
return value; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
export function fetchParameters(method: HttpVerb, body?: any): RequestInit { |
20
|
|
|
const basicHeaders = { |
21
|
|
|
"X-CSRF-TOKEN": csrfToken, |
22
|
|
|
Accept: "application/json", |
23
|
|
|
}; |
24
|
|
|
const jsonBodyHeader = { "Content-Type": "application/json" }; // informs server that the body is a json encoded string |
25
|
|
|
const headers = |
26
|
|
|
body === undefined ? basicHeaders : { ...basicHeaders, ...jsonBodyHeader }; |
27
|
|
|
// We must stringify any object bodies, and ensure dates are formatted as server expects. |
28
|
|
|
const stringBody = |
29
|
|
|
body instanceof Object ? JSON.stringify(body, jsonDateReplacer) : body; |
30
|
|
|
return { |
31
|
|
|
method, |
32
|
|
|
headers, |
33
|
|
|
credentials: "same-origin", // NOTE: This may change if we move to token auth. |
34
|
|
|
...(stringBody && { body: stringBody }), |
35
|
|
|
}; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function defaultFetch( |
39
|
|
|
endpoint: string, |
40
|
|
|
method: HttpVerb, |
41
|
|
|
body?: any, |
42
|
|
|
): Promise<Response> { |
43
|
|
|
return fetch(endpoint, fetchParameters(method, body)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
export function getRequest(endpoint: string): Promise<Response> { |
47
|
|
|
return defaultFetch(endpoint, "GET"); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
export function postRequest(endpoint: string, body: any): Promise<Response> { |
51
|
|
|
return defaultFetch(endpoint, "POST", body); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
export function putRequest(endpoint: string, body: any): Promise<Response> { |
55
|
|
|
return defaultFetch(endpoint, "PUT", body); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
export function deleteRequest(endpoint: string): Promise<Response> { |
59
|
|
|
return defaultFetch(endpoint, "DELETE"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
export class FetchError extends Error { |
63
|
|
|
constructor(public response: Response) { |
64
|
|
|
super(`${response.status} ${response.statusText}`); |
65
|
|
|
/* istanbul ignore next */ |
66
|
|
|
if (Object.setPrototypeOf) { |
67
|
|
|
// Not available in IE 10, but can be polyfilled |
68
|
|
|
Object.setPrototypeOf(this, FetchError.prototype); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
export async function processJsonResponse(response: Response): Promise<Json> { |
73
|
|
|
if (!response.ok) { |
74
|
|
|
throw new FetchError(response); |
75
|
|
|
} |
76
|
|
|
return response.json(); |
77
|
|
|
} |
78
|
|
|
|